/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.service.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.activity.HkShopSeckill;
import com.thinkgem.jeesite.modules.hk.shop.dao.activity.HkShopSeckillDao;
import com.thinkgem.jeesite.modules.hk.shop.entity.activity.HkShopSeckillProduct;
import com.thinkgem.jeesite.modules.hk.shop.dao.activity.HkShopSeckillProductDao;

/**
 * 乐购幸运秒杀Service
 *
 * @author 罗天文
 * @version 2018-05-15
 */
@Service
@Transactional(readOnly = true)
public class HkShopSeckillService extends CrudService<HkShopSeckillDao, HkShopSeckill> {

    @Autowired
    private HkShopSeckillProductDao hkShopSeckillProductDao;
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private final SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final SimpleDateFormat sdf3 = new SimpleDateFormat("HH:mm:ss");

    public HkShopSeckill get(String id) {
        HkShopSeckill hkShopSeckill = super.get(id);
        hkShopSeckill.setHkShopSeckillProductList(hkShopSeckillProductDao.findList(new HkShopSeckillProduct(hkShopSeckill)));
        return hkShopSeckill;
    }

    public List<HkShopSeckill> findList(HkShopSeckill hkShopSeckill) {
        return super.findList(hkShopSeckill);
    }

    public Page<HkShopSeckill> findPage(Page<HkShopSeckill> page, HkShopSeckill hkShopSeckill) {
        return super.findPage(page, hkShopSeckill);
    }

    @Transactional(readOnly = false)
    public void save(HkShopSeckill hkShopSeckill) {

        super.save(hkShopSeckill);
        for (HkShopSeckillProduct hkShopSeckillProduct : hkShopSeckill.getHkShopSeckillProductList()) {
            if (hkShopSeckillProduct.getId() == null) {
                continue;
            }
            if (HkShopSeckillProduct.DEL_FLAG_NORMAL.equals(hkShopSeckillProduct.getDelFlag())) {
                try {
                    hkShopSeckillProduct.setBeginTime(sdf2.parse(sdf.format(hkShopSeckillProduct.getBeginTime()) + " " + hkShopSeckill.getBeginTime()));

                    hkShopSeckillProduct.setEndTime(sdf2.parse(sdf.format(hkShopSeckillProduct.getEndTime()) + " " + hkShopSeckill.getEndTime()));

                } catch (ParseException e) {
                    e.printStackTrace();
                }
                hkShopSeckillProduct.setActivityType("2");
                if (StringUtils.isBlank(hkShopSeckillProduct.getId())) {
                    hkShopSeckillProduct.setSeckill(hkShopSeckill);
                    hkShopSeckillProduct.preInsert();
                    hkShopSeckillProductDao.insert(hkShopSeckillProduct);
                } else {
                    hkShopSeckillProduct.preUpdate();
                    hkShopSeckillProductDao.update(hkShopSeckillProduct);
                }
            } else {
                hkShopSeckillProductDao.delete(hkShopSeckillProduct);
            }
        }
    }

    @Transactional(readOnly = false)
    public void delete(HkShopSeckill hkShopSeckill) {
        super.delete(hkShopSeckill);
        hkShopSeckillProductDao.delete(new HkShopSeckillProduct(hkShopSeckill));
    }

}